package com.union.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;


import com.union.unionht.R;
import com.union.utils.ToastUtils;
import com.union.widget.time.NumericWheelAdapter;
import com.union.widget.time.OnWheelChangedListener;
import com.union.widget.time.WheelView;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;


/**
 * 日期选择�?
 *
 * @author Administrator
 */
public class BirthDateDialog extends Dialog implements
        View.OnClickListener {
    /**
     * 自定义Dialog监听�?
     */
    public interface PriorityListener {
        /**
         * 回调函数，用于在Dialog的监听事件触发后刷新Activity的UI显示
         */
//		public void refreshPriorityUI(String year, String month, String day,
//				String hours, String mins);
        public void refreshPriorityUI(String year, String month, String day);
    }

    private PriorityListener lis;
    private boolean scrolling = false;
    private Context context = null;
    public Button softInfo = null;
    public Button softInfoButton = null;
    private NumericWheelAdapter year_adapter = null;
    private NumericWheelAdapter month_adapter = null;
    private NumericWheelAdapter day_adapter = null;
    private NumericWheelAdapter hours_adapter = null;
    private NumericWheelAdapter mins_adapter = null;
    private NumericWheelAdapter seconds_adapter = null;
    private Button btn_sure = null;
    private Button btn_cancel = null;
    private int curYear = 0;
    private int curMonth = 0;
    private int curDay = 0;
    private int hours = 0;
    private int mins = 0;
    private int seconds = 0;
    private WheelView monthview = null;
    private WheelView yearview = null;
    private WheelView dayview = null;
    //	private WheelView hoursview = null;
//	private WheelView minsview = null;
//	private WheelView secondsview = null;
    private static int theme = R.style.myDialog;// 主题
    private LinearLayout date_layout;
    private int width, height;// 对话框宽高
    private TextView title_tv;
    private String title;
    private int type;
    private String from;
    private String date1;
    private int annual;
    private OnClickCallBack onClickCallBack;
    private String time = "";

    public BirthDateDialog(final Context context,
                           final PriorityListener listener, int currentyear, int currentmonth,
                           int currentday, int width,
                           int height, String title, int type, String from) {
        super(context, theme);
        this.context = context;
        lis = listener;
        this.curYear = currentyear;
        this.curMonth = currentmonth;
        this.curDay = currentday;
        this.width = width;
        this.title = title;
        this.from = from;
//		this.hours = hours;
//		this.mins = mins;
        this.height = height;
        this.type = type;
//		this.seconds = seconds;
        time = currentyear + "-" + currentmonth + "-" + currentday;
    }

    public void setOnClickCallBack(OnClickCallBack onClickCallBack) {
        this.onClickCallBack = onClickCallBack;
    }

    public OnClickCallBack getOnClickCallBack() {
        return onClickCallBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.date_select_wheel);
        btn_sure = (Button) findViewById(R.id.confirm_btn);
        btn_sure.setOnClickListener(this);
        btn_cancel = (Button) findViewById(R.id.cancel_btn);
        btn_cancel.setOnClickListener(this);
        date_layout = (LinearLayout) findViewById(R.id.date_selelct_layout);
        LayoutParams lparams_hours = new LayoutParams(width, height / 3 + 10);
        date_layout.setLayoutParams(lparams_hours);
        title_tv = (TextView) findViewById(R.id.diaolog_title_tv);
        title_tv.setText(title);
        yearview = (WheelView) findViewById(R.id.year);
        monthview = (WheelView) findViewById(R.id.month);
        dayview = (WheelView) findViewById(R.id.day);
        if (type == 0) {
            dayview.setVisibility(View.VISIBLE);
        } else if (type == 1) {
            dayview.setVisibility(View.VISIBLE);
        } else {
            dayview.setVisibility(View.VISIBLE);
        }
        OnWheelChangedListener listener = new OnWheelChangedListener() {
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                if (!scrolling) {
                    updateDays(yearview, monthview, dayview);
                }
            }
        };
        monthview.addChangingListener(listener);
        yearview.addChangingListener(listener);
        Calendar calendar = Calendar.getInstance();
        if (this.curYear == 0 || this.curMonth == 0) {
            curYear = calendar.get(Calendar.YEAR);
            curMonth = calendar.get(Calendar.MONTH) + 1;
            curDay = calendar.get(Calendar.DAY_OF_MONTH);
        }
        // 初始化数�?�?
        annual = Calendar.getInstance().get(Calendar.YEAR);

        if (type == 0) {
            year_adapter = new NumericWheelAdapter(1900, annual);
            yearview.setAdapter(year_adapter);
            int cc = curYear - 1900;// 按下标来�?
            yearview.setCurrentItem(cc);// 传�?过去的是下标
        } else if (type == 1) {
            year_adapter = new NumericWheelAdapter(annual, 2100);
            yearview.setAdapter(year_adapter);
            int cc = curYear - annual;// 按下标来�?
            yearview.setCurrentItem(cc);// 传�?过去的是下标
        } else if (type == 3) {
            year_adapter = new NumericWheelAdapter(1900, 2100);
            yearview.setAdapter(year_adapter);
            int cc = curYear - 1900;// 按下标来�?
            yearview.setCurrentItem(cc);// 传�?过去的是下标
        } else {
//            year_adapter = new NumericWheelAdapter(1950, annual, "年");
            year_adapter = new NumericWheelAdapter(1900, annual); //不需要年的写法
            yearview.setAdapter(year_adapter);
            int cc = curYear - 1900;// 按下标来�?
            yearview.setCurrentItem(cc);// 传�?过去的是下标
        }
        yearview.setVisibleItems(5);
        month_adapter = new NumericWheelAdapter(1, 12, "%02d");
//        month_adapter = new NumericWheelAdapter(1, 12, "%02d");
        monthview.setAdapter(month_adapter);
        monthview.setCurrentItem(curMonth - 1);
        monthview.setCyclic(false);
        monthview.setVisibleItems(5);
        updateDays(yearview, monthview, dayview);
        dayview.setCyclic(false);
        dayview.setVisibleItems(5);

//		hours_adapter = new NumericWheelAdapter(0, 23, "%02d");
//		hoursview.setAdapter(hours_adapter);
//		hoursview.setCurrentItem(hours);
//		hoursview.setCyclic(false);
//		hoursview.setVisibleItems(5);
//		mins_adapter = new NumericWheelAdapter(0, 59, "%02d");
//		minsview.setAdapter(mins_adapter);
//		minsview.setCurrentItem(mins);
//		minsview.setCyclic(false);
//		minsview.setVisibleItems(5);
//		seconds_adapter = new NumericWheelAdapter(0, 59, "%02d");
//		secondsview.setAdapter(seconds_adapter);
//		secondsview.setCurrentItem(seconds);
//		secondsview.setCyclic(false);
//		secondsview.setVisibleItems(5);
    }

    /**
     * 根据年份和月份来更新日期
     */
    private void updateDays(WheelView year, WheelView month, WheelView day) {
        // 添加大小月月份并将其转换为list,方便之后的判�?
        String[] months_big = {"1", "3", "5", "7", "8", "10", "12"};
        String[] months_little = {"4", "6", "9", "11"};

        final List<String> list_big = Arrays.asList(months_big);
        final List<String> list_little = Arrays.asList(months_little);
        int year_num = year.getCurrentItem() + 1900;
        // 判断大小月及是否闰年,用来确定"�?的数�?
        if (list_big.contains(String.valueOf(month.getCurrentItem() + 1))) {
//            day_adapter = new NumericWheelAdapter(1, 31, "%02d");
            day_adapter = new NumericWheelAdapter(1, 31, "%02d");
        } else if (list_little.contains(String.valueOf(month.getCurrentItem() + 1))) {
            day_adapter = new NumericWheelAdapter(1, 30, "%02d");
        } else {
            if ((year_num % 4 == 0 && year_num % 100 != 0)
                    || year_num % 400 == 0)
                day_adapter = new NumericWheelAdapter(1, 29, "%02d");
            else
                day_adapter = new NumericWheelAdapter(1, 28, "%02d");
        }
        dayview.setAdapter(day_adapter);
        dayview.setCurrentItem(curDay - 1);
    }

    public BirthDateDialog(Context context, PriorityListener listener) {
        super(context, theme);
        this.context = context;
    }

    public BirthDateDialog(Context context, String birthDate) {
        super(context, theme);
        this.context = context;

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.confirm_btn) {
            this.dismiss();
        } else {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = sDateFormat.format(new java.util.Date());
            String year = year_adapter.getValues();
            String month = month_adapter.getValues();
            String day = day_adapter.getValues();
            date1 =  year + "-" + month + "-" + day ;
            java.text.DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            try {
                c1.setTime(df.parse(date));
                c2.setTime(df.parse(date1));
                if (type == 0) {
                    int result = c1.compareTo(c2);
                    if (result <= 0) {
                        if (from.equals("PurchaseDetailsAty")) {
                            ToastUtils.custom("采购时间早于当前时间");
                            return;
                        }else if(from.equals("CreateTaskAty1")){
                            ToastUtils.custom("生产日期早于当前时间");
                            return;
                        }
                    }
                }else if(type==1){
                    int result = c1.compareTo(c2);
                    if (result >= 0) {
                        if (from.equals("CreateTaskAty")) {
                            ToastUtils.custom("截止时间晚于当前时间");
                            return;
                        }
                    }
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
            lis.refreshPriorityUI(year, month, day);
            this.dismiss();
            if (onClickCallBack != null) {
                onClickCallBack.confirm();
            }
        }
    }

    public interface OnClickCallBack {
        public void confirm();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
